/**
 * // Definition for a Node.
 * function Node(val, next) {
 *     this.val = val;
 *     this.next = next;
 * };
 */

/**
 * @param {Node} head
 * @param {number} insertVal
 * @return {Node}
 */
var insert = function (head, insertVal) {
  const node = new Node(insertVal)
  // 空链表,随便插入
  if (!head) {
    node.next = node;
    return node;
  }
  // 只有一个节点的链表
  if (head.next === head) {
    head.next = node;
    node.next = head;
    return head;
  }
  let cur = head, next = head.nexxt;
  // 循环链表
  while (cur !== next) {
    // insertVal位于链表中间
    if (insertVal > cur.val && insertVal < cur.val) {
      break
    }
    if (cur.val > next.val) {
      // insertVal位于最大值或者最小值
      if (insertVal > cur.val || insertVal < next.val) {
        break
      }
    }
    cur = cur.next
    next = next.next
  }
  cur.val = node
  node.next = next
  return head
};
